
import java.util.*;

// 定义Book类，实现Comparable接口
class Book implements Comparable<Book> {
    private double price;

    public Book(double price) {
        this.price = price;
    }

    // getter和setter方法
    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    // 实现compareTo方法，按照价格比较书籍
    @Override
    public int compareTo(Book otherBook) {
        return Double.compare(this.price, otherBook.price);
    }
}

public class BookComparatorDemo {
    public static void main(String[] args) {
        // 创建一个LinkedList来存储Book对象
        List<Book> bookList = new LinkedList<>();

        // 添加一些Book对象到列表中
        bookList.add(new Book(29.99));
        bookList.add(new Book(19.95));
        bookList.add(new Book(24.50));

        // 创建一个新的Book对象
        Book newBook = new Book(24.50);

        // 使用Collections.sort对列表进行排序
        Collections.sort(bookList);

        // 打印排序后的列表
        System.out.println("Sorted list of books:");
        for (Book book : bookList) {
            System.out.println(book.getPrice());
        }

        // 检查新创建的Book对象是否与列表中的任何对象相等
        boolean isEqual = false;
        for (Book book : bookList) {
            if (book.compareTo(newBook) == 0) {
                isEqual = true;
                break;
            }
        }

        // 输出结果
        if (isEqual) {
            System.out.println("The new book is equal to one in the list.");
        } else {
            System.out.println("The new book is not equal to any in the list.");
        }
    }
}

